import store from 'store2';

import type { StoreBase } from 'store2';

const generateStorageUtils = (store: StoreBase) => ({
  get<T>(key: string): T {
    return store.get(key);
  },

  set(key: string, value: unknown) {
    // 如果value=undefined就删除掉数据，否则在get调用时会获取到一个字符串的'undefined'值，而不是undefined
    if (value === undefined) {
      store.remove(key);
    } else {
      store.set(key, value);
    }
  },

  remove(key: string) {
    store.remove(key);
  },
});

const storeInstance = store.namespace('uCode-store'); // 设置名称空间（前缀）

export const localStorageInstance = generateStorageUtils(storeInstance);
export const sessionStorageInstance = generateStorageUtils(storeInstance.session);
